import random
import KBEngine
import KBEDebug as log

class AvatarTask(KBEngine.EntityComponent):

    def __init__(self):
        pass

    # region kbengine method
    def onAttached(self, owner):
        log.DEBUG_MSG(f'AvatarTask onAttached: owner={owner.id}')

    def onDetached(self, owner):
        log.DEBUG_MSG(f'AvatarTask onDetached: owner={owner.id}')

    def onClientEnabled(self):
        log.DEBUG_MSG(f'AvatarTask onClientEnabled owner={self.owner.id}')
        
    def onClientDeath(self):
        log.DEBUG_MSG(f'AvatarTask onClientDeath owner={self.owner.id}')
    
    def completeTask(self, taskId, gold):
        if taskId == 1:
            pass
        elif taskId == 2:
            self.todayInviteFriend += 1
            pass
        elif taskId == 3:
            self.completeNormal = 1
        elif taskId == 4:
            self.completeHard = 1
        elif taskId == 5:
            self.completeExtraTask(5, gold)
        elif taskId == 6:
            self.completeExtraTask(6, gold)
        elif taskId == 7:
            self.completeExtraTask(7, gold)
        elif taskId == 8:
            self.completeExtraTask(8, gold)
        else:
            self.completeExtraTask(taskId, gold)
        self.checkTaskState(taskId)
    
    def checkTaskState(self, taskId):
        today_task_lst = self.todayCompletedTask
        if 1 not in today_task_lst: # 每日登录
            today_task_lst.append(1)
            self.owner.avatarOther.coin += 50
            self.owner.avatarOther.point += 5
        if 2 not in today_task_lst: # 邀请好友
            if self.todayInviteFriend >= 2:
                today_task_lst.append(2)
                self.owner.avatarOther.coin += 2000
                self.owner.avatarOther.point += 10
        if 3 not in today_task_lst: # 完成普通关卡
            if self.completeNormal == 1:
                today_task_lst.append(3)
                self.owner.avatarOther.coin += 300
                self.owner.avatarOther.point += 10
        if 4 not in today_task_lst: # 完成困难关卡
            if self.completeHard == 1:
                today_task_lst.append(4)
                self.owner.avatarOther.coin += 500
                self.owner.avatarOther.point += 10
        self.todayCompletedTask = today_task_lst
        self.client.onCompleteDailyTask(self.todayCompletedTask, taskId)
        
    
    def resetDailyTask(self):
        self.todayCompletedTask = []
        self.todayInviteFriend = 0
        self.completeNormal = 0
        self.completeHard = 0
        self.checkTaskState(0)
        
    def completeExtraTask(self, taskId, gold):
        if taskId not in self.completedExtraTask:
                self.completedExtraTask.append(taskId)
                taskAddCoin = gold
                self.owner.avatarOther.coin += taskAddCoin
                self.completedExtraTask = self.completedExtraTask
                self.client.onCompleteDailyTask(self.todayCompletedTask, taskId)
                self.client.onCompleteExtraTask(taskAddCoin)
                if taskId > 9:
                    sql = f'UPDATE lobby_task_info SET count = count + 1 WHERE taskId = \"{taskId}\"'
                    log.DEBUG_MSG(f'更新任务完成人数: {taskId}')
                    KBEngine.executeRawDatabaseCommand(sql, None)
    
        